import { message, Select } from 'antd';
import { addon, Permission } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import { request } from "src/business/util_tool";
import { AppServiceUtility } from 'src/projects/app/appService';
import { table_param } from "src/projects/app/util-tool";
import { ROUTE_PATH } from 'src/projects/router';

let { Option } = Select;
/**
 * 组件：服务记录信息状态
 */
export interface ServiceRecordViewState extends ReactViewState {
    /** 服务订单列表 */
    // service_order_list?: [];
    /** 服务项目列表 */
    service_project_list?: [];
    /** 服务人员列表 */
    service_worker_list?: [];
    /** 选择行Ids */
    select_ids?: string[];
    /** 接口名 */
    request_ulr?: string;
}

/**
 * 组件：服务记录信息
 * 描述
 */
export class ServiceRecordView extends ReactView<ServiceRecordViewControl, ServiceRecordViewState> {
    private columns_data_source = [{
        title: '服务订单编号',
        dataIndex: 'service_order_code',
        key: 'service_order_code',
    }, {
        title: '服务项目',
        dataIndex: 'project.name',
        key: 'project.name',
    }, {
        title: '服务开始时间',
        dataIndex: 'start_date',
        key: 'start_date',
    }, {
        title: '服务完成时间',
        dataIndex: 'end_date',
        key: 'end_date',
    }, {
        title: '服务人员',
        dataIndex: 'service_worker_id',
        key: 'service_worker_id',
    }, {
        title: '服务状态',
        dataIndex: 'status',
        key: 'status',
    }, {
        title: '计价金额',
        dataIndex: 'valuation_amount',
        key: 'valuation_amount',
    }, {
        title: '是否已回访',
        dataIndex: 'is_huifang',
        key: 'is_huifang',
    },
    ];
    constructor(props: any) {
        super(props);
        this.state = {
            // service_order_list: [],
            service_project_list: [],
            service_worker_list: [],
            request_ulr: '',
        };
    }
    /** 发送费用记录按钮 */
    sendRecord = () => {
        let ids = this.state.select_ids;
        if (ids && ids!.length > 0) {
            request(this, AppServiceUtility.service_record_service.send_record!(ids))
                .then((data: any) => {
                    if (data === 'Success') {
                        message.info('发送成功');
                    } else {
                        message.info('发送失败');
                    }
                }).catch((error: Error) => {
                    message.error('发送失败！' + error.message);
                });
        }
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        if ('icon_edit' === type) {
            console.log('自定义按钮edit返回值：', contents);
            this.props.history!.push(ROUTE_PATH.changeServiceRecord + '/' + contents.id);
        }
    }
    componentWillMount() {
        let request_ulr = this.props.request_url;
        this.setState({
            request_ulr
            // condition: [{ personnel_category: personnelCategory.elder }, 1, 10] // 长者资料
        });
    }
    componentDidMount() {
        request(this, AppServiceUtility.user_service.get_user_list!({}, 1, 999))
            .then((datas: any) => {
                this.setState({
                    service_worker_list: datas.result,
                });
            });
        request(this, AppServiceUtility.service_operation_service.get_service_item_list!({}))
            .then((datas: any) => {
                this.setState({
                    service_project_list: datas.result,
                });
            });
        // request(this, AppServiceUtility.service_operation_service.get_service_order_list!({}))
        //     .then((datas: any) => {
        //         this.setState({
        //             service_order_list: datas.result,
        //         });
        //     });
    }
    render() {
        let worker = this.state.service_worker_list;
        let service_worker_list: any[] = [];
        worker!.map((item, idx) => {
            service_worker_list.push(<Option key={item['user_id']}>{item['name']}</Option>);
        });
        // let service_order_list: any[] = [];
        // this.state.service_order_list!.map((item, idx) => {
        //     service_order_list.push(<Option key={item['id']}>{item['id']}</Option>);
        // });
        let service_project_list: any[] = [];
        this.state.service_project_list!.map((item, idx) => {
            service_project_list.push(<Option key={item['id']}>{item['name']}</Option>);
        });
        let service_record = {
            type_show: false,
            edit_form_items_props: [
                {
                    type: InputType.input,
                    label: "服务订单编号",
                    decorator_id: "service_order_code",
                    option: {
                        placeholder: "请输入服务订单编号",
                        // childrens: service_order_list,
                    }
                }, {
                    type: InputType.rangePicker,
                    label: "开始时间范围",
                    decorator_id: "start_date",
                    option: {
                        placeholder: "请输入开始时间范围",
                        // childrens: service_order_list,
                    }
                }, {
                    type: InputType.rangePicker,
                    label: "完成时间范围",
                    decorator_id: "end_date",
                    option: {
                        placeholder: "请输入完成时间范围",
                        // childrens: service_order_list,
                    }
                }, {
                    type: InputType.input,
                    label: "服务人员",
                    decorator_id: "service_worker_id",
                    option: {
                        placeholder: "请输入服务人员",
                        // childrens: service_order_list,
                    }
                }, {
                    type: InputType.input,
                    label: "服务项目",
                    decorator_id: "service_project_name",
                    option: {
                        placeholder: "请选择服务项目",
                        childrens: service_project_list,
                    }
                },
            ],
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.service_record_service,
            service_option: {
                select: {
                    service_func: this.state.request_ulr,
                    service_condition: [{}, 1, 10]
                },
                delete: {
                    service_func: 'delete_service_record'
                }
            },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.edit_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let service_record_list = Object.assign(service_record, table_param);
        return (
            <SignFrameLayout {...service_record_list} />
        );
    }
}

/**
 * 控件：服务项目登记信息
 * 描述
 */
@addon('ServiceRecordView', '服务项目登记信息', '描述')
@reactControl(ServiceRecordView, true)
export class ServiceRecordViewControl extends ReactViewControl {
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
    public permission?: Permission;
}